// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({env: cloud.DYNAMIC_CURRENT_ENV})
const db = cloud.database()
const _ = db.command
const dbUser = db.collection('user')

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()

    var obj =  await dbUser.where({openid:event.openid}).get()
    //没传入openid，返回null
    if(!event.openid){
        return null
    }
    //查询时出错，返回null
    if(obj['errMsg'] !== "collection.get:ok"){
        return null
    }
    //获取用户所有信息
    if(event.action == "getAll"){
        return obj['data'][0]
    }
    //获取用户积分
    else if(event.action == "getCredit"){
        return obj['data'][0]['credit']
    }
    //获取用户发布的任务ID
    else if(event.action == "getTaskPID"){
        return obj['data'][0]['taskListPublish']
    }
    //获取用户领取的任务ID
    else if(event.action == "getTaskRID"){
        return obj['data'][0]['taskListReceive']
    }else{
        return null
    }
}